//
//  TDFinanceViewController.m
//  TFB
//
//  Created by Nothing on 15/3/16.
//  Copyright (c) 2015年 TD. All rights reserved.
//

#import "TDMoreViewController.h"
#import "TDUseHelpTableViewController.h"
#import "TDAboutMineViewController.h"
#import "TDAgreementViewController.h"
#import "TDLoginViewController.h"

#define CALL_ALERT_TAG    100

@interface TDMoreViewController () <UITableViewDataSource, UITableViewDelegate>
{
    NSArray *_cellTextArray;
    NSArray *_cellImageArray;

}
@end

@implementation TDMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:31/255.0f green:121/255.0f blue:132/255.0f alpha:1.0f];
    self.navigationItem.title = @"更多";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    _cellTextArray = @[@"联系客服", @"关于我们"];
    _cellImageArray = @[@"more_kefu", @"more_about"];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 1, SVFS.width, 60)];
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logoutBtn.frame = CGRectMake(10, 16, SVFS.width - 20, 44);
    [logoutBtn setBackgroundImage:[UIImage imageNamed:@"bg_btn"] forState:0];
    [logoutBtn setTitle:@"安全退出" forState:0];
    [logoutBtn setTitleColor:[UIColor whiteColor] forState:0];
    [logoutBtn addTarget:self action:@selector(onClickLogoutBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:logoutBtn];

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.autoresizingMask = self.view.autoresizingMask;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableFooterView = footerView;
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getProvBtnClick:(id)sender {
    
   
}

- (IBAction)getCityBtnClick:(id)sender {
}

- (void)onClickLogoutBtn:(id)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestForLoginoutWithCustMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] custId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] complete:^(BOOL succeed, NSString *msg, NSString *cod) {
        NSLog(@"退出登陆：%@", msg);
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        TDLoginViewController *loginVC = [[TDLoginViewController alloc] init];
        UINavigationController *loginNav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [loginNav.navigationBar setBarStyle:UIBarStyleBlack];
        self.view.window.rootViewController = loginNav;
    }];
}

#pragma mark-------
#pragma mark   TableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cellTextArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndef = @"cellIndef";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndef];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndef];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 26, 26)];
        imageView.tag = 1001;
        [cell.contentView addSubview:imageView];
        
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.frame = CGRectMake(imageView.frame.size.width + imageView.frame.origin.x + 8, 2, 80, 40);
        textLabel.font = [UIFont systemFontOfSize:15];
        textLabel.tag = 1002;
        [cell.contentView addSubview:textLabel];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        rightLabel.frame = CGRectMake(CGRectGetMaxX(textLabel.frame) + 8, 2, self.tableView.bounds.size.width - 10 - CGRectGetMaxX(textLabel.frame) - 8, 40);
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.font = [UIFont systemFontOfSize:15];
        //154 170 170
        rightLabel.textColor = [UIColor colorWithRed:154/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
        rightLabel.tag = 1003;
        [cell.contentView addSubview:rightLabel];
    }
    
    UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:1001];
    imageView.image = [UIImage imageNamed:_cellImageArray[indexPath.row]];
    
    UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:1002];
    textLabel.text = _cellTextArray[indexPath.row];
    
    UILabel *rightLabel = (UILabel *)[cell.contentView viewWithTag:1003];
    if (indexPath.row == 0) {
        rightLabel.text = KEFU_PNONENUM;
    } else {
        rightLabel.text = [NSString stringWithFormat:@"V%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (indexPath.row == 0) {
        //联系客服
        UIAlertView *callAlertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"确定要拨打%@么？", KEFU_PNONENUM] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拨打", nil];
        callAlertView.tag = CALL_ALERT_TAG;
        callAlertView.delegate = self;
        [callAlertView show];
    } else {
        //关于我们
        TDAboutMineViewController *aboutUsVC = [[TDAboutMineViewController alloc] init];
        aboutUsVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:aboutUsVC animated:YES];
    }
    
    /*
    switch (indexPath.row) {
        case 0:
        {
            //分享
            [self share];
          
        }
            break;
        case 1:
        {
            //意见反馈
            

        }
            break;
        case 2:
        {
            //使用帮助
            TDUseHelpTableViewController *useHelpVC = [[TDUseHelpTableViewController alloc] init];
            useHelpVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:useHelpVC animated:YES];
        }
            break;
        case 3:
        {
            //联系客服
            UIAlertView *callAlertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"确定要拨打%@么？", KEFU_PNONENUM] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拨打", nil];
            callAlertView.tag = CALL_ALERT_TAG;
            callAlertView.delegate = self;
            [callAlertView show];
            
        }
            break;
        case 4:
        {
            //版本更新

        }
            break;
        case 5:
        {
            //关于我们
            TDAboutMineViewController *aboutUsVC = [[TDAboutMineViewController alloc] init];
            aboutUsVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
            break;
            
        default:
            break;
    }
    */
    
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        return;
    }
    
    if (alertView.tag == CALL_ALERT_TAG) {
        if (buttonIndex == 1) {
            NSLog(@"拨打");
            NSString *deviceType = [UIDevice currentDevice].model;
            if([deviceType  isEqualToString:@"iPod touch"]||[deviceType  isEqualToString:@"iPad"]||[deviceType  isEqualToString:@"iPhone Simulator"]){
                NSLog(@"该设备不可拨打电话");
                [self.view makeToast:@"该设备不可拨打电话" duration:1.5f position:@"center"];
                return;
            }
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", KEFU_PNONENUM]]];
        }
    }
}

- (void)share
{
    NSArray *activityItems = [[NSArray alloc]initWithObjects:@"棠付宝", nil];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:Nil];
    UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType, BOOL completed) {
        if (completed) {
            NSLog(@"completed");
        } else
        {
            NSLog(@"cancled");
        }
        [activityVC dismissViewControllerAnimated:YES completion:Nil];
    };
    activityVC.completionHandler = myBlock;
    [self presentViewController:activityVC animated:YES completion:Nil];
}








@end
